package com.yamaha.bikemarket.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SCStaticFunc {

	private static String userId;
	private static String email;
	private static String registerId;
	private static String fbAccessToken;
	private static int LAST_PAGE;
	private static JSONObject data;
	private static Map<String, Object> map = new HashMap<String, Object>();

	public static final String BAND = "BRAND";
	public static final String COLOR = "COLOR";
	public static final String PROVINCE = "PROVINCE";
	public static final String COMMENT = "COMMENT";
	public static final String YEAR = "YEAR";

	private final static String PACKAGE = "com.yamaha.bikemarket";

	public static void setData(final Context context, final String name,
			final JSONObject value) {
		map.put(name, value);

		LogUtil.v("SetData", "Name : " + name);
		LogUtil.v("SetData", "value: " + value);

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString(name, value.toString());
		editor.commit();
	}

	public static void setData(final Context context, final String name,
			final String value) {
		map.put(name, value);

		LogUtil.v("SetData", "Name : " + name);
		LogUtil.v("SetData", "value: " + value);

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString(name, value.toString());
		editor.commit();
	}

	public static void setData(final Context context, final String name,
			final JSONArray value) {
		map.put(name, value);

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString(name, value.toString());
		editor.commit();
	}

	public static Object getData(final Context context, final String name)
			throws JSONException {
		Object v = map.get(name);

		if (v == null) {
			SharedPreferences savedSession = context.getSharedPreferences(
					PACKAGE, Context.MODE_PRIVATE);
			final String temp = savedSession.getString(name, null);

			if (null != temp) {
				try {
					v = new JSONObject(temp);
				} catch (JSONException e) {
					try {
						v = new JSONArray(temp);
					} catch (JSONException ex) {
						v = temp;
					}
				}
			}
		}

		LogUtil.v("getData", "name : " + name);
		LogUtil.v("getData", "value : " + ((v != null) ? v.toString() : "NULL"));

		return v;
	}

	public static void setUserId(final Context context, final String userId) {
		SCStaticFunc.userId = userId;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString("userId", userId);
		editor.commit();
	}

	public static String getUserId(final Context context) {
		if (userId == null) {
			SharedPreferences savedSession = context.getSharedPreferences(
					PACKAGE, Context.MODE_PRIVATE);
			userId = savedSession.getString("userId", null);
		}

		return userId;
	}

	public static void setEmail(final Context context, final String email) {
		SCStaticFunc.email = email;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString("userEmail", email);
		editor.commit();
	}

	public static String getEmail(final Context context) {
		if (email == null) {
			SharedPreferences savedSession = context.getSharedPreferences(
					PACKAGE, Context.MODE_PRIVATE);
			email = savedSession.getString("userEmail", null);
		}

		return email;
	}

	public static void setRegisterId(final Context context,
			final String registerId) {
		SCStaticFunc.registerId = registerId;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString("userRegId", userId);
		editor.commit();
	}

	public static String getRegisterId(final Context context) {

		if (registerId == null) {
			SharedPreferences savedSession = context.getSharedPreferences(
					PACKAGE, Context.MODE_PRIVATE);
			registerId = savedSession.getString("userRegId", null);
		}
		return registerId;
	}

	public static void setFbAccessToken(final Context context,
			final String fbAccessToken) {
		SCStaticFunc.fbAccessToken = fbAccessToken;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putString("fbAccessToken", fbAccessToken);
		editor.commit();
	}

	public static String getFbAccessToken(final Context context) {

		if (fbAccessToken == null) {
			SharedPreferences savedSession = context.getSharedPreferences(
					PACKAGE, Context.MODE_PRIVATE);
			fbAccessToken = savedSession.getString("fbAccessToken", null);
		}
		return fbAccessToken;
	}

	public static void setLastPageIndex(final Context context, final int index) {
		SCStaticFunc.LAST_PAGE = index;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		editor.putInt("last_page", index);
		editor.commit();
	}

	public static int getLastPageIndex(final Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE);
		// LAST_PAGE = savedSession.getInt("last_page", MainActivity.LOGIN);

		return LAST_PAGE;
	}

	public static void setData(final Context context, final JSONObject data) {
		SCStaticFunc.data = data;

		Editor editor = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE).edit();
		if (null != data) {
			editor.putString("data", data.toString());
		} else {
			editor.putString("data", null);
		}
		editor.commit();
	}

	public static JSONObject getData(final Context context)
			throws JSONException {
		SharedPreferences savedSession = context.getSharedPreferences(PACKAGE,
				Context.MODE_PRIVATE);
		final String jsonString = savedSession.getString("data", null);

		final JSONObject object;
		if (null != jsonString) {
			object = new JSONObject(jsonString);
			SCStaticFunc.data = object;
		} else {
			object = null;
		}

		return object;
	}
}
